/*
 * University Of Washington - Tacoma
 * TCSS-422, Operating Systems
 * Assignment 1 - Web Crawler
 * April 26, 2011
 * 
 * Team: Kernel Monkeys
 * Andrew Boguk
 * Sergiy Kulyk
 * Nicholas Swartzendruber
 */

package crawler.engine;

import static org.junit.Assert.*;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.List;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

import org.junit.Before;
import org.junit.Test;

public class PageRetrieverTest {
	
	BlockingQueue<List<String>> my_q;
	PageRetriever my_retriever;
	byte[] my_data;

	@Before
	public void setUp() throws Exception {
		my_q = new LinkedBlockingQueue<List<String>>();
		my_retriever = new PageRetriever(new URL("http://www.russianwa.com/public/test.html"), my_q);
		my_data = "This is a page content".getBytes();
	}

	
	/**
	 * Test the readStream() method.
	 */
	@Test
	public void testReadStream() {
		try {
	        InputStream input = new ByteArrayInputStream(my_data);
	        final byte[] page = my_retriever.readStream(my_data.length, input);
	        
	        for (int i = 0; i < my_data.length; i++) {
	        	assertEquals("Retrieved byte does not match the original.", 
	            		my_data[i], page[i]);
	        }        
			
		} catch (final IOException e) {
			fail("Got IOException");
		}
	}
	
	
	/**
	 * Test the run() method.
	 */
	@Test
	public void testRun() {
		
		final String[] page = {
				"www.russianwa.com/public/test.html",
				"<html>",
				"<head>",
				"</head>",
				"<body>",
				"Kernel Monkeys.",
				"</body>",
				"</html>"
		};
		
		my_retriever.run();
		final List<String> content = my_q.poll();
		
		int i = 0;
		while (!my_q.isEmpty()) {
			assertEquals("", page[i], content.remove(0));
			i++;
		}
	}

}
